<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <template>
          <div class="employees-container">
            <div class="app-container">
              <page-tools>
                <template #left>
                  <span>总记录数: 16 条</span>
                </template>
                <template #right>
                  <el-button v-allow="'import_employee'" type="warning" size="small" @click="$router.push('/import')">excel导入</el-button>
                  <el-button type="danger" size="small" @click="hExport">excel导出</el-button>
                  <el-button type="primary" size="small" @click="showDialog=true">新增员工
                  </el-button>
                </template>
              </page-tools>

              <el-card style="margin-top: 10px;">
                <el-table border :data="list" :default-sort="{prop: 'workNumber'}">
                  <el-table-column label="序号" type="index" />
                  <el-table-column label="姓名" prop="username" />
                  <el-table-column label="头像">
                    <template slot-scope="scope">
                      <image-holder :src="scope.row.staffPhoto" />
                    </template>
                  </el-table-column>
                  <el-table-column label="工号" prop="workNumber" />
                  <el-table-column label="聘用形式" prop="formOfEmployment">
                    <template slot-scope="scope">
                      类型：{{ formatEmployment(scope.row.formOfEmployment) }}
                    </template>
                  </el-table-column>
                  <el-table-column label="部门" prop="departmentName" />
                  <el-table-column label="入职时间" prop="timeOfEntry" :sortable="true">
                    <template slot-scope="scope">
                      {{ scope.row.timeOfEntry.slice(0,10) }}
                    </template>
                  </el-table-column>
                  <el-table-column label="操作" width="280">
                    <template slot-scope="scope">
                      <el-button type="text" size="small" @click="$router.push('/employees/detail/?id='+scope.row.id)">查看</el-button>
                      <el-button type="text" size="small" @click="hAssignRole(scope.row.id)">分配角色</el-button>
                      <el-button type="text" size="small" @click="helfn(scope.row.id)">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
                <!-- 分页组件 -->
                <el-row type="flex" justify="center" align="middle" style="height: 60px">
                  <el-pagination
                    layout="sizes,prev, pager, next,jumper"
                    :total="total"
                    :page-size="params.size"
                    :page-sizes="[2,5,10]"
                    :current-page.sync="curPage"
                    @current-change="hCurrentChange"
                    @size-change="hSizeChange"
                  />
                </el-row>
              </el-card>
            </div>
          </div>
        </template>
        <el-dialog title="新增员工" :visible.sync="showDialog" @close="hCloseDialog">
          <addor-edit ref="AddEmployee" @close="hclose" @gengxin="hgengxin" />
        </el-dialog>
        <el-dialog title="角色分配" :visible.sync="showDialogRole">
          <AssignRole :id="curid" ref="assignRole" @close="showDialogRole=false" />
        </el-dialog>
      </el-card>
    </div>

  </div>
</template>
<script>
import { getEmployeeList, delEmployee } from '@/api/employees.js'
import addorEdit from './empDialog.vue'
import AssignRole from './assignRole.vue'
export default {
  components: {
    addorEdit,
    AssignRole
  },
  data() {
    return {
      curid: '', // 分配角色需要的id
      curPage: 1, // 当前的页码-设置给分页组件的
      showDialog: false,
      showDialogRole: false,
      list: [],
      total: 0, // 总共数据条数
      params: {
        page: 1,
        size: 2
      }
    }
  },
  computed: {
    // 是不是满的
    maxpage() {
      return this.total % this.params.size === 0
    },
    // 最大页码
    islastmaxPage() {
      return Math.ceil(this.total / this.params.size)
    }
  },

  created() {
    this.loadEmployee()
  },
  methods: {
    // 分配角色显示
    hAssignRole(id) {
      this.curid = id
      this.showDialogRole = true
      this.$nextTick(() => {
        this.$refs.assignRole.loadRoles()
      })
    },
    // 通知父组件重新获取数据
    hgengxin() {
      if (this.maxpage) {
        this.params.page = this.islastmaxPage + 1
      } else {
        this.params.page = this.islastmaxPage
      }
      this.loadEmployee()
    },
    async loadEmployee() {
      const res = await getEmployeeList(this.params)
      this.list = res.data.rows

      this.total = res.data.total
      this.curPage = this.params.page
    },
    formatEmployment(val) {
      if (val === 1) {
        return '正式'
      } else if (val === 2) {
        return '非正式'
      } else {
        return '未知'
      }
    },
    // 切换页码方法
    hCurrentChange(val) {
      this.params.page = val
      this.loadEmployee()
    },
    // 每条显示几条数据
    hSizeChange(val) {
      this.params.size = val
      this.loadEmployee()
    },
    async doDel(id) {
      await delEmployee(id)
      if (this.list.length === 1) {
        this.params.page--
        if (this.params.page <= 0) {
          this.params.page = 1
        }
      }
      this.loadEmployee()
    },
    helfn(id) {
      // console.log(id)
      this.$confirm('确认删除吗？').then(() => {
        this.doDel(id)
      }).catch(() => {})
    },
    hclose() {
      this.showDialog = false
    },
    hCloseDialog() {
      this.$refs.AddEmployee.resetForm()
    },
    format(res) {
      const hmap = {
        'id': '编号',
        'password': '密码',
        'mobile': '手机号',
        'username': '姓名',
        'timeOfEntry': '入职日期',
        'formOfEmployment': '聘用形式',
        'correctionTime': '转正日期',
        'workNumber': '工号',
        'departmentName': '部门',
        'staffPhoto': '头像地址'
      }

      const header = Object.keys(res[0]).map(obj => hmap[obj])
      const data = res.map(obj => Object.values(obj))
      console.log(header, data)
      return { header, data }
    },
    // 导出exclx功能
    async hExport() {
      import('@/vendor/Export2Excel').then(async excel => {
        // 1.发ajax发请求拿请求
        await this.loadEmployee()
        // 2.做数据格式转换
        console.log(this.list)
        const { header, data } = this.format(this.list)
        excel.export_json_to_excel({
          // header: ['姓名', '工资'], // 表头 必填
          header: header, // 表头 必填
          data: data,
          // data: [
          //   ['刘备11111111111111', 100],
          //   ['关羽', 500]
          // ], // 具体数据 必填
          filename: '员工信息' // 文件名称

        })
      })
    }
  }
}
</script>
